package com.example.report.controller;

import com.example.report.entity.ReportExcel;
import com.example.report.service.ReportService;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/")
public class ReportController {

    @Resource(name = "ReportServiceImpl")
    private ReportService reportService;


    @RequestMapping("/reportInfo")
    public String reportInfo(@ModelAttribute ReportExcel reportExcel, HttpServletRequest request,
                             @RequestParam(value="pageNum",defaultValue="1")int pageNum,
                             @RequestParam(value="pageSize",defaultValue="5")int pageSize){


        PageInfo<ReportExcel> pageInfo =  reportService.queryData(reportExcel,pageNum,pageSize);
        request.setAttribute("pageInfo",pageInfo);
        return "report/contribution";
    }


    @RequestMapping("/exportInfo")
    public void exportInfo(@ModelAttribute ReportExcel reportExcel, HttpServletResponse response,
                             @RequestParam(value="pageNum",defaultValue="1")int pageNum,
                             @RequestParam(value="pageSize",defaultValue="100")int pageSize){


        PageInfo<ReportExcel> pageInfo =  reportService.queryData(reportExcel,pageNum,pageSize);
        List<ReportExcel>  list = pageInfo.getList();
        //导出操作
       /* EasyPoiUtil.exportExcel(list,"客户订单统计表","客户订单统计",
                ReportExcel.class,"客户订单统计表.xls",response);*/

        try {
            // 告诉浏览器用什么软件可以打开此文件
            response.setHeader("content-Type", "application/vnd.ms-excel");
            // 下载文件的默认名称
            response.setHeader("Content-Disposition", "attachment;filename="+ "orderInfo" +".xls");

          //  response.setHeader("Content-Disposition", "attachment;filename=客户订单统计表" + URLEncoder.encode("客户订单统计表","UTF-8") + ".xls");
            //编码
            response.setCharacterEncoding("UTF-8");

            Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), ReportExcel.class, list);
            workbook.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }



}
